<template>
  <div>
    <el-dialog
      :title="doEdit?'操作':'查看'"
      ref="tableDialog"
      :visible.sync="dialogVisible"
      width="80%"
      v-if="formData"
      @close="close"
    >
      <!-- <el-tabs v-model="tabIndex" type="card">
        <el-tab-pane label="申请单信息" name="1"></el-tab-pane>
        <el-tab-pane label="历史流程" name="2"></el-tab-pane>
        <el-tab-pane label="操作" name="3" v-if="doEdit"></el-tab-pane>
      </el-tabs>-->
      <el-form :model="formData" label-width="100px" ref="form">
        <el-row>
          <el-col :sm="12">
            <el-form-item label="申请人">{{formData.userName}}</el-form-item>
          </el-col>
          <el-col :sm="12">
            <el-form-item label="所在单位">{{formData.companyName}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :sm="12">
            <el-form-item label="申请时间">{{formData.createtime}}</el-form-item>
          </el-col>
          <el-col :sm="12">
            <el-form-item label="用车人数">{{formData.pcount}}人</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="24">
            <el-form-item label="用车理由">{{formData.reason}}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="12">
            <el-form-item label="开始时间">{{formData.starttime}}</el-form-item>
          </el-col>
          <el-col :sm="12">
            <el-form-item label="结束时间">{{formData.endtime}}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="12">
            <el-form-item label="始发地">{{formData.origin}}</el-form-item>
          </el-col>
          <el-col :sm="12">
            <el-form-item label="目的地">{{formData.destination}}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="24">
            <el-form-item label="申请备注">{{formData.reason}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :sm="24">
            <el-form-item label="状态">{{formData.processType}}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="历史流程">
            <el-table :data="history" style="width: 100%" size="mini" border>
              <el-table-column prop="taskName" label="步骤" align="center"></el-table-column>
              <el-table-column prop="userName" label="审核人" align="center"></el-table-column>
              <el-table-column prop="mobile" label="电话" align="center"></el-table-column>
              <el-table-column prop="mark" label="审批意见" align="center"></el-table-column>
              <el-table-column prop="passStr" label="状态" align="center"></el-table-column>
              <el-table-column prop="optime" label="操作时间" align="center"></el-table-column>
            </el-table>
          </el-form-item>
        </el-row>
        <!-- 
        <template v-if="formData.driverId">
          <el-divider>调度信息</el-divider>
          <el-row>
            <el-col :sm="12">
              <el-form-item label="司机姓名">{{formData.driverName}}</el-form-item>
            </el-col>
            <el-col :sm="12">
              <el-form-item label="车辆">{{formData.carName}}</el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :sm="12">
              <el-form-item label="接送时间">{{formData.jstime}}</el-form-item>
            </el-col>
            <el-col :sm="12">
              <el-form-item label="接送地点">{{formData.jsaddress}}</el-form-item>
            </el-col>
          </el-row>
        </template>-->
      </el-form>
      <el-form label-width="80px">
        <template v-if="doEdit">
          <el-row>
            <el-col :sm="24">
              <el-form-item label="是否批准">
                <el-radio-group v-model="pass">
                  <el-radio :label="1">批准</el-radio>
                  <el-radio :label="0">不批准</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </template>
        <template v-if="opData.formKey=='ddusecar'&&doEdit">
          <el-row style="margin-top: 10px;">
            <el-col :sm="24">
              <el-form-item label="接送时间">
                <el-date-picker v-model="jstime" type="datetime" placeholder="选择接送时间"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row
            v-for="(usecaritem, index) in formData.usecaritems"
            :key="index"
            style="margin-top: 10px;"
          >
            <el-col :sm="24">
              <el-form-item :label="'车辆'+(index+1)">
                <el-input
                  v-model="usecaritem.carCode"
                  style="width:200px;"
                  placeholder="请选择车辆"
                  @click.native="selectCar(index)"
                ></el-input>
                <el-input
                  v-model="usecaritem.driverName"
                  style="width:200px;"
                  placeholder="请选择司机"
                  @click.native="selectDriver(index)"
                ></el-input>
                {{usecaritem.ztStr}}
                <el-button
                  type="danger"
                  size="small"
                  circle
                  @click="removeUseItem(index)"
                  v-if="formData.usecaritems.length>1"
                >
                  <i class="el-icon-minus"></i>
                </el-button>
                <el-button type="primary" size="small" circle @click="addUseItem">
                  <i class="el-icon-plus"></i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="margin-top: 10px;">
            <el-col :sm="24">
              <el-form-item label="接送地点">
                <el-input v-model="jsaddress" placeholder="请输入接送地点"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </template>
        <el-row v-if="doEdit&&type==0" style="margin-top: 10px;">
          <el-col :sm="24">
            <el-form-item label="备注">
              <el-input v-model="formData.shenhemark" type="textarea" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer" style="text-align: left;">
        <el-button @click="close">关闭</el-button>
        <el-button type="primary" @click="save" v-if="doEdit&&type==0">保存</el-button>
        <el-button type="primary" @click="update" v-if="doEdit&&type==2">更新调度信息</el-button>
      </span>
    </el-dialog>
    <selectDriver ref="selectDriver"></selectDriver>
    <selectCar ref="selectCar"></selectCar>
  </div>
</template>
<script>
import { viewJson, sh, diaodu, mytaskListjson, driverqr } from "@/api/task";
import { findDetails } from "@/api/company";
import { bz } from "@/api/carbz";
import { update } from "@/api/usecar";
import { listJson } from "@/api/car";
import { listJson as driverListJson } from "@/api/driver";
import { dateFormat } from "@/utils/utils";
import selectDriver from "./select-driver";
import selectCar from "./select-car";
export default {
  components: { selectDriver, selectCar },
  data() {
    return {
      driverId: null,
      pass: 1,
      tabIndex: "1",
      jstime: "",
      carId: null,
      jsaddress: "",
      shenhemark: "",
      opData: null,
      history: [],
      formData: null,
      doEdit: false,
      dialogVisible: false,
      type: 0, //0待办任务 2历史任务
      carList: [], //车辆选择列表
      driverList: [], //司机选择列表
    };
  },
  methods: {
    selectDriver(index) {
      this.$refs.selectDriver.show(
        index,
        this.formData.usecaritems.map((e) => e.driverId)
      );
    },
    selectCar(index) {
      this.$refs.selectCar.show(
        index,
        this.formData.usecaritems.map((e) => e.carId)
      );
    },
    close() {
      this.dialogVisible = false;
    },
    addUseItem() {
      this.formData.usecaritems.push({
        driverId: null,
        carId: null,
        driverName: null,
        carCode: null,
      });
    },
    removeUseItem(index) {
      this.formData.usecaritems.splice(index, 1);
    },
    /**
     * @description 操作任务
     * @param opData 当前操作的任务原始数据 list接口返回的
     * @param type 当前列表的类型 0待办任务 2历史任务 2类型时候，只有调度员可编辑且不能再修改备注了
     */
    editTask(opData, type) {
      this.viewTask(opData, type);
      this.doEdit = true;
      this.tabIndex = "3";
      this.type = type;
    },
    /**
     * @description 查看任务
     * @param opData 当前操作的任务原始数据 list接口返回的
     * @param type 当前列表的类型 0待办任务 2历史任务 2类型时候，只有调度员可编辑且不能再修改备注了
     */
    async viewTask(opData, type) {
      this.type = type;
      this.doEdit = false;
      this.tabIndex = "1";
      this.opData = opData;
      let params = {
        tableId: opData.tableId,
        pid: opData.processInstanceId,
        taskId: opData.taskId,
        formKey: opData.formKey,
      };
      let r = await viewJson(params);
      this.dialogVisible = true;
      let padding = r.usecar.carcount - r.usecar.usecaritems.length;
      r.usecar.usecaritems.forEach((element) => {
        element.carCode = element.carcode;
        element.driverId = element.driverid;
        element.carId = element.carid;
      });
      for (let i = 0; i < padding; i++) {
        r.usecar.usecaritems.push({
          driverId: null,
          carId: null,
          driverName: null,
          carCode: null,
        });
      }
      this.jstime = r.usecar.starttime;
      this.jsaddress = r.usecar.origin;
      this.formData = r.usecar;
      this.history.splice(0, this.history.length);
      this.history.push(...r.history);
    },
    //处理流程
    save() {
      let param = {};
      param["pass"] = this.pass ? "true" : "false";
      param["mark"] = this.formData.shenhemark || "";
      param["tableId"] = this.opData.tableId;
      param["taskId"] = this.opData.taskId;
      param["formKey"] = this.opData.formKey;
      param["pid"] = this.opData.processInstanceId;
      if (this.opData.formKey == "shusecar") {
        this.sh(param);
      } else if (this.opData.formKey == "ddusecar") {
        if (this.pass) {
          let emptyIndex = this.formData.usecaritems.findIndex((usecaritem) => {
            if (!usecaritem.carId || !usecaritem.driverId) {
              return true;
            } else {
              return false;
            }
          });
          if (emptyIndex >= 0) {
            this.$message.warning(
              `第${emptyIndex + 1}个车辆没有配置好，请配置好再提交`
            );
            return;
          }
          if (!this.jstime) {
            this.$message.warning("请选择接送时间");
            return;
          } else if (!this.jsaddress) {
            this.$message.warning("请输入接送地点");
            return;
          }
          param["jstime"] = dateFormat(this.jstime);
          param["jsaddress"] = this.jsaddress;
          param["json"] = JSON.stringify(
            this.formData.usecaritems.map((e) => {
              return { "driver.id": e.driverId, "car.id": e.carId };
            })
          );
        } else {
          param["json"] = "[]";
        }
        this.diaodu(param);
      } else if (this.opData.formKey == "driverusecae") {
        this.driverqr(param);
      }
    },
    //更新流程 只有调度员调度后可以修改
    async update() {
      if (this.opData.formKey != "ddusecar") {
        this.$message({
          message: "当前类型任务不支持更新操作!",
          type: "error",
        });
        return;
      }
      if (this.formData.usecaritems.length == 0) {
        this.$message.warning(`必须选择至少1个司机`);
        return;
      }
      let emptyIndex = this.formData.usecaritems.findIndex((usecaritem) => {
        if (!usecaritem.carId || !usecaritem.driverId) {
          return true;
        } else {
          return false;
        }
      });
      if (emptyIndex >= 0) {
        this.$message.warning(
          `第${emptyIndex + 1}个车辆没有配置好，请配置好再提交`
        );
        return;
      }
      let param = {};
      param["tableId"] = this.opData.tableId;
      param["jstime"] = dateFormat(this.jstime);
      param["jsaddress"] = this.jsaddress;
      param["json"] = JSON.stringify(
        this.formData.usecaritems.map((e) => {
          return { "driver.id": e.driverId, "car.id": e.carId };
        })
      );
      let r = await update(param);
      if (r && r.error == 0) {
        this.$message({
          message: "更新成功",
          type: "success",
        });
        this.finish();
      } else {
        this.$message({
          message: "更新失败",
          type: "error",
        });
      }
    },
    //审核
    async sh(params) {
      let d = await sh(params);
      if (d.error == 0) {
        this.$message("操作成功");
        this.finish();
      } else {
        this.$message("操作失败");
      }
    },
    //调度
    async diaodu(params) {
      let d = await diaodu(params);
      if (d.error == 0) {
        this.$message("操作成功");
        this.finish();
      } else {
        this.$message("操作失败");
      }
    },
    //司机确认
    async driverqr(params) {
      let d = await driverqr(params);
      if (d.error == 0) {
        this.$message("操作成功");
        this.finish();
      } else {
        this.$message("操作失败");
      }
    },
    //各种操作完成后关闭对话框刷新父组件列表
    finish() {
      this.close();
      this.$parent.fetchData();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-timeline {
  position: relative;
  top: -25px;
  left: 60px;
  margin-bottom: -45px;
}
</style>